﻿// See https://aka.ms/new-console-template for more information
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Threading.Channels;

//ConnectionFactory factory = new ConnectionFactory()
//{
//    UserName = "yzerp",
//    Password = "yzerp",
//    HostName = "192.168.1.251",
//    VirtualHost = "/",
//    Port=5672
//};

//using (var connection = factory.CreateConnection())
//{
//    //创建通道
//    using (var channel = connection.CreateModel())
//    {
//        channel.ExchangeDeclare(exchange: "examine_direct", type: "direct");
//        var queueName = channel.QueueDeclare().QueueName;
//        channel.QueueBind(queueName, "examine_direct", routingKey: "examine-message-webSocket");
//        //时间基本消费者
//        EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

//        //接收到消息事件
//        consumer.Received += (ch, ea) =>
//        {
//            var message = Encoding.UTF8.GetString(ea.Body.ToArray());
//            Console.WriteLine("收到消息：" + message);
//            //确认该消息已被消费
//            channel.BasicAck(ea.DeliveryTag, false);
//        };

//        channel.BasicQos(0,1,false);
//        //启动消费者，设置为手动应答消息
//        channel.BasicConsume("examine_direct", false, consumer);
//        Console.WriteLine("消费者3已启动");
//        Console.ReadKey();
//    }
//}

var factory = new ConnectionFactory()
{
    HostName = "192.168.1.251",
    Port = 5672,
    Password = "yzerp",
    UserName = "yzerp",
    VirtualHost = "/"
};
var connection = factory.CreateConnection();
var channel = connection.CreateModel();
channel.ExchangeDeclare(exchange: "examine_direct", type: "direct");
var queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queueName, "examine_direct", routingKey: "examine-message-webSocket");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += async (model, ea) =>
{
    var result = Encoding.UTF8.GetString(ea.Body.ToArray());
    await Console.Out.WriteLineAsync(result);
    channel.BasicAck(ea.DeliveryTag, true);
};
channel.BasicQos(0, 1, false);
channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
Console.WriteLine("消费者3已启动");
Console.ReadKey();